package com.cuje.coolrtc.activity;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cuje.coolrtc.App;
import com.cuje.coolrtc.R;
import com.cuje.coolrtc.base.BaseActivity;
import com.p2p.core.P2PFacade;
import com.p2p.define.P2PRoomType;
import com.p2p.listener.P2PGlobalListener;
import com.p2p.listener.P2PRoomListener;
import com.p2p.model.P2PError;
import com.p2p.model.P2PRoom;
import com.p2p.model.P2PUser;

import org.webrtc.MediaStream;

import java.util.List;

public class DataActivity extends BaseActivity implements P2PGlobalListener {
    private Context mContext;
    private P2PRoom mRoom;
    private TextView chatContent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data);
        mContext = this;
        P2PFacade.onCreate(this, App.getUid(), App.getUserName());
        chatContent = findViewById(R.id.tv_chat_content);
        findViewById(R.id.bt_send_msg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText et = findViewById(R.id.et_send_msg);
                String msg = et.getText().toString();
                mRoom.dataChannelTalk(msg);
            }
        });
    }

    @Override
    public void onInitSuccess() {
        P2PFacade.joinRoom("1234", P2PRoomType.Text, new P2PRoomListener() {
            @Override
            public boolean checkPermission() {
                return false;
            }

            @Override
            public void onJoinSuccess(P2PRoom room) {
                mRoom = room;
            }

            @Override
            public void onStatusChanged(final P2PUser user, final String newStatus) {
                Log.e(TAG, user.getName() + "-onStatusChanged-" + newStatus);
            }

            @Override
            public void onLocalStream(MediaStream localStream) {

            }

            @Override
            public void onAddRemoteStream(P2PUser user) {

            }

            @Override
            public void onRemoveRemoteStream(P2PUser user) {

            }

            @Override
            public void onDataChannelTextMessage(final P2PUser p2PUser, final String message) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        chatContent.append(p2PUser.getName() + "-onDataChannelTextMessage-" + message + "\n");
                    }
                });

            }

            @Override
            public void onUserJoined(final P2PUser user) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        chatContent.append(user.getName() + "-onUserJoined \n");
                    }
                });
            }

            @Override
            public void onUserLeft(final P2PUser user) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        chatContent.append(user.getName() + "-onUserLeft \n");
                    }
                });
            }
        });
    }

    @Override
    public void onInitFailure(P2PError error) {

    }

    @Override
    public void onRoomCreateSuccess(P2PRoom room) {

    }

    @Override
    public void onRoomCreateFailure(P2PRoom room, P2PError error) {

    }

    @Override
    public void onLeaveRoomSuccess(P2PRoom room, P2PUser user) {

    }

    @Override
    public void onLeaveRoomFailure(P2PRoom room, P2PError error) {

    }

    @Override
    public void onGetUserListSuccess(P2PRoom room, List<P2PUser> userList) {

    }

    @Override
    public void onGetUserListFailure(P2PRoom room, P2PError error) {

    }

    @Override
    public void onGetRoomListSuccess(String groupId, List<P2PRoom> roomList) {

    }

    @Override
    public void onGetRoomListFailure(String groupId, P2PError error) {

    }


    @Override
    protected void onPause() {
        super.onPause();
        if (mRoom != null) {
            mRoom.onPause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mRoom != null) {
            mRoom.onResume();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mRoom != null) {
            mRoom.onDestroy();
        }
    }
}
